/*----------------------------------------------------------------------------*\
| /gamemode/infection.lua              | infection system                | SHD |
\*----------------------------------------------------------------------------*/

zday.infection = {}
if CLIENT then
	INFECTING = false
end

function zday.infection.IsInfected( ply )
	return ply:GetNetworkedBool( "infected" )
end

local nextupdate = CurTime() + 1
function zday.infection.Think()
	if SERVER then
		if CurTime() > nextupdate then
			nextupdate = CurTime() + 1
			for k,v in pairs( player.GetAll() ) do
				if zday.infection.IsInfected( v ) then
					v:TakeDamage( 1, v )
				end
			end
		end
	elseif CLIENT then
		zday.PP.ColourMod(zday.PP.Actual)
	end
end
hook.Add( "Think", "ZdayInfectionThink", zday.infection.Think )

if SERVER then
	function zday.infection.Infect( ply )
		ply:SetNetworkedBool( "infected", true )
	end
	
	function zday.infection.InfectStart( ply )
		timer.Create( ply:GetName() .. "infected", 20, 1, zday.infection.Infect, ply )
		umsg.Start("zday_startinfecting", ply)
		umsg.End() 
		
	end

	function zday.infection.Cure( ply )
		ply:SetNetworkedBool( "infected", false )
		umsg.Start("zday_stopinfecting", ply)
		umsg.End() 
		if timer.IsTimer( ply:GetName() .. "infected" ) then
			timer.Destroy( ply:GetName() .. "infected" )
		end			
	end
	
	function zday.infection.PlayerTakeDamage( ent, inflictor, attacker, amount )
		if !ent:IsValid() or !ent:IsPlayer() then return end
		if attacker:GetClass() == "npc_zombie" or attacker:GetClass() == "npc_zombie_torso" then
			if (ent:Armor() < 50) then
				local lucky = math.Rand( 0, 100 - math.Max(( ent:Armor() * 10 ),100))
				print(amount)
				if lucky > 85 and amount >= 0 then
					zday.infection.InfectStart( ent )
				end
			end
		end
	end
	hook.Add( "EntityTakeDamage", "zday.infection.PlayerTakeDamage", zday.infection.PlayerTakeDamage)
end
